<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\Notification;
use Carbon\Carbon;


/**
 * @title 消息模块(2.0)
 * @description 接口说明
 * @header name:accessToken require:1 default: desc:accessToken
 */

class NotificationController extends ApiController
{
    /**
     * @title 我的消息
     * @description 接口说明
     * @author wjx
     * @url /api/noticeIndex
     * @method POST
     * 
     * @return title:消息类型
     * @return type:消息类型ID
     * @return count:未读数量
     */
    public function noticeIndex(Request $request)
    {
        $user = $request->user;   
        $data = [];   
        $data[0]['title'] = '公告消息';
        $data[0]['type'] = 1;
        $data[0]['count'] = $user->notifications()->where('type',1)->whereNull('read_at')->count();
        $data[1]['title'] = '通知消息';
        $data[1]['type'] = 2;
        $data[1]['count'] = $user->notifications()->where('type',2)->whereNull('read_at')->count();
        $data[2]['title'] = '点赞通知';
        $data[2]['type'] = 3;
        $data[2]['count'] = $user->notifications()->where('type',3)->whereNull('read_at')->count();
                
        
        return $this->success($data);
    }
    /**
     * @title 公告消息
     * @description 接口说明
     * @author wjx
     * @url /api/publish
     * @method POST
     * 
     * @param name:type type:string require:1 default:refresh other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:ID type:int require:1 default:-1 other: desc:数据起点ID
     */
    public function publish(Request $request)
    {
        $user = $request->user;
        $offset = $request->offset ? $request->offset : 10;
        $startId = $request->ID ? $request->ID : 0;
        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->type;
        $msgType = $request->msgType;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('type参数错误');
        }

        if ($loadType == 'more') {
            $notice = Notification::filters([['user_id','=',$user->id],['type','=',1], ['id', '<', $startId]])->take($offset)->orderBy('id','desc')->get();
        } else {
            $notice = Notification::filters([['user_id','=',$user->id],['type','=',1], ['id', '>', $startId]])->take($offset)->orderBy('id','desc')->get();
        }
        $notice->transform(function($item){
            $notice = $item->data;
            return [
                'id' => $item->id,
                'title' => $notice['title'],
                'content' => $notice['content'],
                'time' => $notice['time'],
            ];
        });
        Notification::where([['user_id','=',$user->id],['type','=',1]])->whereNull('read_at')->update(['read_at'=>Carbon::now()]);

        return $this->success($notice);
    }
    /**
     * @title 通知消息
     * @description 接口说明
     * @author wjx
     * @url /api/notice
     * @method POST
     * 
     * @param name:type type:string require:1 default:refresh other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:ID type:int require:1 default:-1 other: desc:数据起点ID
     */
    public function notice(Request $request)
    {
        $user = $request->user;
        $offset = $request->offset ? $request->offset : 10;
        $startId = $request->ID ? $request->ID : 0;
        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->type;
        $msgType = $request->msgType;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('type参数错误');
        }

        if ($loadType == 'more') {
            $notice = Notification::filters([['user_id','=',$user->id],['type','=',2], ['id', '<', $startId]])->take($offset)->orderBy('id','desc')->get();
        } else {
            $notice = Notification::filters([['user_id','=',$user->id],['type','=',2], ['id', '>', $startId]])->take($offset)->orderBy('id','desc')->get();
        }
        $notice->transform(function($item){
            $notice = $item->data;
            return [
                'id' => $item->id,
                'title' => $notice['title'],
                'content' => $notice['content'],
                'time' => $notice['time'],
            ];
        });
        Notification::where([['user_id','=',$user->id],['type','=',2]])->whereNull('read_at')->update(['read_at'=>Carbon::now()]);

        return $this->success($notice);
    }
    /**
     * @title 点赞消息
     * @description 接口说明
     * @author wjx
     * @url /api/flower
     * @method POST
     * 
     * @param name:type type:string require:1 default:refresh other: desc:方法参数:more/refresh
     * @param name:offset type:int require:1 default:10 other: desc:方法参数:每页数据量
     * @param name:ID type:int require:1 default:-1 other: desc:数据起点ID
     */
    public function flower(Request $request)
    {
        $user = $request->user;
        $offset = $request->offset ? $request->offset : 10;
        $startId = $request->ID ? $request->ID : 0;
        $offset = $request->offset ? $request->offset : 10;
        if ($offset > 25) {
            $offset = 25;
        }
        $loadType = $request->type;
        $msgType = $request->msgType;
        if (!in_array($loadType, ['more', 'refresh'])) {
            return $this->failed('type参数错误');
        }

        if ($loadType == 'more') {
            $notice = Notification::filters([['user_id','=',$user->id],['type','=',3], ['id', '<', $startId]])->take($offset)->orderBy('id','desc')->get();
        } else {
            $notice = Notification::filters([['user_id','=',$user->id],['type','=',3], ['id', '>', $startId]])->take($offset)->orderBy('id','desc')->get();
        }
        $notice->transform(function($item){
            $notice = $item->data;
            return [
                'id' => $item->id,
                'icon' => $notice['icon'],
                'title' => $notice['title'],
                'content' => $notice['content'],
                'time' => $notice['time'],
            ];
        });
        Notification::where([['user_id','=',$user->id],['type','=',3]])->whereNull('read_at')->update(['read_at'=>Carbon::now()]);

        return $this->success($notice);
    }

}
